/**
 * A simple SMS send and receive demo.
 * 
 * Copyright ?1998-2005 Research In Motion Ltd. 
 */


import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.io.*;
import net.rim.device.api.system.*;
import com.rim.samples.device.resource.*;
import net.rim.device.api.i18n.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import javax.wireless.messaging.*;

/**
 * <p>A simple demo of SMS send and receive. This program requires an associated server component,  you can find this
 * component at com.rim.samples.server.smsdemo
 * <p>This application makes use of SIGNED APIs and therefore requires signing to function on the device.
 */
 
public final class SmsScreen extends MainScreen {
{
    
    //members ------------------------------------------------------------------
    private RichTextField
    private static String _openString = "sms://:0"; //see Connector implementation notes

  
    //inner classes ------------------------------------------------------------
    private class ListeningThread extends Thread
    {
        private boolean _stop = false;
        private MessageConnection _mc;

        public synchronized void stop()
        {
            _stop = true;
            try {
                _mc.close(); //close the connection so the thread will return.
            } catch (IOException e) {
                System.err.println(e.toString());
            }
        }

        public void run()
        {
            try {
                 System.out.println("about to getting message");
                _mc = (MessageConnection)Connector.open(_openString); //closed by the stop() method
                for(;;)
                {
                    if ( _stop )
                    {
                        return;
                    }
                     System.out.println("getting message");
                    Message m = _mc.receive();
                    System.out.println("got message");

                    receivedSmsMessage(m);
                }
            } catch (IOException e)
            {
                //likely the stream was closed
                System.err.println(e.toString());
            }
        }
    }

    /**
     * a simple abstraction of an sms message, used by the SendThread class
     */
    private static final class SmsMessage
    {
        private String _address;
        private String _msg;

        public SmsMessage(String address, String msg)
        {
            _address = address;
            _msg = msg;
        }
        
        public Message toMessage(MessageConnection mc)
        {
            TextMessage m = (TextMessage)mc.newMessage(MessageConnection.TEXT_MESSAGE,"//" + _address + ":3590");
            m.setPayloadText(_msg);
            return m;
        }
    }
  

    /**
     * Update the GUI with the data just received
     */
    private void updateSmsScreen(final String msg)
    {
        invokeLater(new Runnable() {
            public void run()
            {
               
            }
        });

    }
    /**
     * some simple formatting for a received sms message
     */
    private void receivedSmsMessage(Message m)
    {
        String address = m.getAddress();
        String msg = null;
        if ( m instanceof TextMessage )
        {
            TextMessage tm = (TextMessage)m;
            msg = tm.getPayloadText();
        }   

        updateStatus(sb.toString());
    }
}
